<?php


namespace app\index\controller;

use think\Controller;
use think\facade\Env;

class Xsd extends Controller
{
    public function analysis()
    {
        // 读取Product.xsd
        $dirFile = $this->dirFile();
        // 读取amzn-base.xsd
        foreach ($dirFile as $key => $value) {
            if (strpos($value, "amzn-base.xsd")) {
                $file = $value;
                break;
            }
        }
        $base = $this->xsdArr($file);
        $element = $base['element'];
        $base = $this->node($element);

        foreach ($dirFile as $key => $value) {
            if (strpos($value, "Home.xsd")) {
                $file = $value;
                break;
            }
        }
        $Product = $this->xsdArr($file);
        $element = $Product['element'];
        $ProductArr = $element['complexType']['sequence']['element'];
        $data = $this->node($ProductArr);

    }

    // 读取节点
    private function node($data)
    {
        $list = array();
        foreach ($data as $key => $value) {
            $arr = array();
            if (array_key_exists('@attributes', $value)) {
                $arr1 = $value['@attributes'];
                if (array_key_exists('ref', $arr1)) {
                    $arr['key'] = $arr1['ref'];
                } else if (array_key_exists('name', $arr1)) {
                    $arr['key'] = $arr1['name'];
                }
                $arr = array_merge($arr, $arr1);
            }
            if (array_key_exists('complexType', $value)) {
                $complexType = $value['complexType'];
                //判断是否有下级节点
                if (array_key_exists('sequence', $complexType)) {
                    $sequence = $complexType['sequence'];
                    if (array_key_exists('element', $sequence)) {
                        $element = $sequence['element'];
                        $arr['element'] = $this->node($element);
                    }
                }
                if (array_key_exists('choice', $complexType)) {
                    $choice = $complexType['choice'];
                    if (array_key_exists('element', $choice)) {
                        $element = $choice['element'];
                        foreach ($element as $k => $v) {
                            $arr3[] = $v['@attributes']['ref'];
                        }
                    }
                    $arr['choice'] = $arr3;
                }
            } else if (array_key_exists('sequence', $value)) {
                $sequence = $value['sequence'];
                if (array_key_exists('element', $sequence)) {
                    $element = $sequence['element'];
                    $arr['element'] = $this->node($element);
                }
            }
            if (array_key_exists('simpleType', $value)) {
                //判断是否有选项
                $simpleType = $value['simpleType'];
                if (array_key_exists('restriction', $simpleType)) {
                    $restriction = $simpleType['restriction'];
                    if (array_key_exists('enumeration', $restriction)) {
                        $enumeration = $restriction['enumeration'];
                        $arr2 = array();
                        foreach ($enumeration as $k => $v) {
                            $arr2[] = $v['@attributes']['value'];
                        }
                        $arr['value'] = $arr2;
                    }
                }
            }
            $key = $arr['key'];
            unset($arr['key']);
            $list[$key] = $arr;
        }
        return $list;
    }

    // 把xsd解析成对象或数组
    public function xsdArr($file)
    {
        $xmlFile = str_replace('xsd', 'xml', $file);
        $doc = new \DOMDocument();
        $doc->preserveWhiteSpace = true;
        $doc->load($file);
        $doc->save($xmlFile);
        $myxmlfile = file_get_contents($xmlFile);
        $parseObj = str_replace($doc->lastChild->prefix . ':', "", $myxmlfile);
        $xml_string = simplexml_load_string($parseObj);
        $json = json_encode($xml_string);
        $data = json_decode($json, true);
        return $data;
    }

    //读取全部xsd 路径
    public function dirFile()
    {
        $public = Env::get('root_path') . "public/xsd";
        $dir = getDirContent($public);
        $dirArr = array();
        foreach ($dir as $key => $value) {
            $arr = explode('.', $value);
            if (strtolower($arr[count($arr) - 1]) == "xsd") {
                $dirArr[] = $public . "/" . $value;
            }
        }
        return $dirArr;
    }
}